//
//  LandViewController.m
//  小汽车
//
//  Created by mill on 15/1/8.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "LandViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "Singleton.h"
#import "UserDetailModel.h"
@interface LandViewController ()<UIAlertViewDelegate>

@end

@implementation LandViewController

- (void)dealloc
{
    [_sign release];
    [_uid release];
    [_profileImage release];
    [_nickname release];
    [_registTime release];
    [_sex release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1]];
    self.navigationItem.title = @"登陆";
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    [self createLand];
}
-(void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createLand
{
    UILabel *other = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, self.view.frame.size.width / 4, self.view.frame.size.height / 20)];
    [other setText:@"其他登录方式:"];
    other.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:other];
    [other release];
    
    UIImage *image = [UIImage imageNamed:@"sina.png"];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(10, 5, 20, 20);
    imageView.backgroundColor = [UIColor whiteColor];
    
    UIButton *sina = [[UIButton alloc] initWithFrame:CGRectMake(20,other.frame.size.height + 10, self.view.frame.size.width  / 2, self.view.frame.size.height / 20)];
    [sina setTitle:@"新浪微博登陆" forState:UIControlStateNormal];
    sina.backgroundColor = [UIColor whiteColor];
    [sina setTitleColor:[UIColor colorWithRed:17.0/255.0 green:190.0/255.0 blue:227.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    [sina addTarget:self action:@selector(sinaLand:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sina];
    [sina addSubview:imageView];
    [sina release];
    [imageView release];
}
-(void)sinaLand:(id)sender{
    
    Singleton *share = [Singleton shareInstance];
    
    [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
        if (result ) {
            NSLog(@"授权登陆成功，已获取用户信息");
            NSTimeInterval regAt = [userInfo regAt];
            NSInteger a = [userInfo gender];
            
            if (a == 0) {
                self.sex = @"男";
            }else if (a == 1){
                self.sex = @"女";
            }else if (a == 2){
                self.sex = @"未知";
            }
            self.uid = [userInfo uid];
            self.nickname = [userInfo nickname];
            self.profileImage = [userInfo profileImage];
            
            NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
            [formatter setDateStyle:NSDateFormatterMediumStyle];
            [formatter setTimeStyle:NSDateFormatterShortStyle];
            [formatter setDateFormat:@"YYYY-MM-dd"];
            NSDate *dd = [NSDate dateWithTimeIntervalSince1970:regAt];
            self.registTime = [formatter stringFromDate:dd];
            
            if (share.special == NO) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"恭喜您" message:[NSString stringWithFormat:@"登陆成功,用户ID:%@,昵称:%@,头像:%@",_uid,_nickname,_profileImage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                NSLog(@"sex = %@",_sex);
                share.special = YES;
            }
        }
        else{
            NSLog(@"分享失败,错误码:%ld,错误描述%@",(long)[error errorCode],[error errorDescription]);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"很遗憾" message:@"登陆失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            share.special = NO;
        }
    }];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        //始终保持登录状态
        //        [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
        [self.navigationController popViewControllerAnimated:YES];
        [self.delegate changeViewStateUid:self.uid nicname:self.nickname profileImage:self.profileImage sex:self.sex registTime:self.registTime];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
